package com.hyjx.framework.component.upload;

import com.hyjx.framework.common.Constants;
import com.hyjx.framework.common.util.SpringBeanUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 上传附件控制类
 *
 * @author dijian
 */
@Controller
@RequestMapping("/upload")
public class UploadController {
    
    
    private UploadService service;

    /**
     * 文件上传
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/doupload", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String handleFileUpload(MultipartHttpServletRequest request, HttpServletResponse response) {
        UploadService service = (UploadService) SpringBeanUtil.getBean(Constants.UPLOAD_SERVICE_NAME);
        if (service.add(request, response)) {
            return "success";
        } else {
            return "error";
        }
    }

    /**
     * 进入到信息查询页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoList", method = RequestMethod.GET)
    public String gotoList(HttpServletRequest request) {
        request.setAttribute("type", request.getParameter("type"));
        return "/framework/upload/list";
    }

    @RequestMapping(value = "/gotoImageList", method = {RequestMethod.POST, RequestMethod.GET})
    public ModelAndView gotoImageList(HttpServletRequest request) {
        UploadModel model = new UploadModel();
        model.setVchr_originname(request.getParameter("vchr_originname"));
        model.setVchr_domain(request.getParameter("vchr_domain"));
        
        ModelAndView mav = new ModelAndView("/framework/upload/imagelist");
        return mav;
    }

    /**
     * 执行客户查询操作
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryList", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String queryList(UploadModel model, HttpServletRequest request) {
        String type = request.getParameter("type");
        if (type != null && type.equals("2")) {
            model.setVchr_filetype("vchr_originname like '%.jpg' or vchr_originname like '%.png' or vchr_originname like '%.bmp' or vchr_originname like '%.gif'");
        }
        service = (UploadService) SpringBeanUtil.getBean(Constants.UPLOAD_SERVICE_NAME);
        return service.queryList(model);
    }

    /**
     * 删除信息
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoDelete", method = RequestMethod.GET)
    @ResponseBody
    public String gotoDelete(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        service = (UploadService) SpringBeanUtil.getBean(Constants.UPLOAD_SERVICE_NAME);
        return service.delete(ids) > 0 ? "success" : "failure";
    }

    /**
     * 信息查看
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoView", method = RequestMethod.GET)
    public ModelAndView gotoView(HttpServletRequest request) {
        String id = request.getParameter("id");
        UploadModel model = null;
        service = (UploadService) SpringBeanUtil.getBean(Constants.UPLOAD_SERVICE_NAME);
        model = (UploadModel) service.getInfoById(id);
        ModelAndView mav = new ModelAndView("/framework/upload/view");
        mav.addObject("model", model);
        String type = request.getParameter("type");
        request.setAttribute("type", type);
        return mav;
    }

    @RequestMapping(value = "/download", produces = "application/json; charset=utf-8", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String download(HttpServletRequest request, HttpServletResponse response) {
        service = (UploadService) SpringBeanUtil.getBean(Constants.UPLOAD_SERVICE_NAME);
        service.download(request, response);
        return null;
    }

    @RequestMapping(value = "/view", produces = "application/json; charset=utf-8", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String view(HttpServletRequest request, HttpServletResponse response) {
        service = (UploadService) SpringBeanUtil.getBean(Constants.UPLOAD_SERVICE_NAME);
        service.view(request, response);
        return null;
    }

    /**
     * 通过url请求返回图像的字节流
     */
    @RequestMapping(value = "/getPreview", method = RequestMethod.GET)
    @ResponseBody
    public void getIcon(String cateogry, HttpServletRequest request,
                        HttpServletResponse response) throws IOException {
        service = (UploadService) SpringBeanUtil.getBean(Constants.UPLOAD_SERVICE_NAME);
        service.getIcon(cateogry, request, response);
    }

    /**
     * request
     *
     * @return
     */
    @RequestMapping(value = "/loadImage", produces = "application/text; charset=utf-8", method = RequestMethod.GET)
    @ResponseBody
    public String queryCurrentPics(HttpServletRequest request) {
        service = (UploadService) SpringBeanUtil.getBean(Constants.UPLOAD_SERVICE_NAME);
        return service.queryCurrentPics(request);
    }


    @RequestMapping(value = "/doUploadCapture", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String handleFileUpload(HttpServletRequest request, HttpServletResponse response) {
        try {
            service = (UploadService) SpringBeanUtil.getBean(Constants.UPLOAD_SERVICE_NAME);
            service.addCapture(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}